package cn.dlc.com.product.controller;

import cn.dlc.com.product.entity.Product;
import cn.dlc.com.product.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("product")
public class ProductController {

    @Autowired
    private ProductService productService;

    @Value("${server.port}")
    private String port;

    @Value("${spring.cloud.client.ip-address}")
    private String ip;

    @GetMapping("{id}")
    public Product findById(@PathVariable("id") String id) {

        System.out.println("访问的地址是：" + ip + ":" + port);

        System.out.println("id: " + id);
        if(!"".equals(id) && id != null) {
            try {
                Product product = productService.findById(id);
                product.setDesc("访问的地址是：" + ip + ":" + port);
                return product;
            }catch (Exception e) {
                e.printStackTrace();
                return null;
            }
        }
        return new Product();
    }
}
